/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.building;

import cz.fi.muni.diplomka.common.Coordinates;
import cz.fi.muni.diplomka.common.enumeration.BuildingType;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.servicelayer.dto.BuildingInCity;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityBuildingManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityBuildingManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.BuildingConvertor;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

/**
 *
 * @author Jakub Kutil
 */
public class UpgradePanel extends Panel {
    
    private CityBuildingManager cityBuildingManager = new CityBuildingManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private BuildingConvertor buildingConvertor = new BuildingConvertor();
    private int x = 0;
    private int y = 0;
    private Long cityId = new Long(0);
    private int level = 0;
    private String name = "";
    
    public UpgradePanel(String id, final PageParameters parameters) {
        super(id);
        
        if (parameters.containsKey("cityId")) {
            cityId = Long.parseLong(parameters.getString("cityId"));
        }
        
        if (parameters.containsKey("x")) {
            x = Integer.parseInt(parameters.getString("x"));
        }
        
        if (parameters.containsKey("y")) {
            y = Integer.parseInt(parameters.getString("y"));
        }
        
        if (parameters.containsKey("name")) {
            name = parameters.getString("name");
        }
        
        if (parameters.containsKey("level")) {
            level = Integer.parseInt(parameters.getString("level"));
        }
        
        BuildingConvertor bc = new BuildingConvertor();
        final BuildingInCity b = bc.getBuildingFromXML(name);
        
        add(new Label("upgradeInfo", "Costs -  Food:" + b.getLevels().get(level + 1).getPriceFood() + " Gold:" + b.getLevels().get(level + 1).getPriceGold()
                + " Stone:" + b.getLevels().get(level + 1).getPriceStone()
                + " Time:" + b.getLevels().get(level + 1).getPriceTime()));
        String requires = "Requires: ";
        for (BuildingType key : b.getLevels().get(level + 1).getRequires().keySet()) {
            requires += key + " level: " + b.getLevels().get(level + 1).getRequires().get(key).toString() + " ";
        }
        add(new Label("requires", requires));
        final Coordinates c = new Coordinates();
        c.setX_coordinate(x);
        c.setY_coordinate(y);
        
        if (cityBuildingManager.getBuildingInProgressAtPosition(cityId, c).getCityId() != null) {
            add(new Label("upgrading", "Building is beign upgraded, time of finish:" + cityBuildingManager.getBuildingInProgressAtPosition(cityId, c).getTimeOfFinish()));
        } else {
            add(new Label("upgrading", ""));
        }
        
        Form<?> form = new Form<Void>("form") {
            @Override
            protected void onSubmit() {
                
                if (cityBuildingManager.getBuildingInProgressAtPosition(cityId, c).getCityId() != null) {
                    info("Building is already being upgraded");
                } else {
                    boolean ok = true;
                    boolean requirements = false;
                    if (b.getLevels().get(level + 1).getPriceFood() > cityResourcesManager.getResourceCount(cityId, ResourceType.Food)) {
                        ok = false;
                        error("You do not have enough food to upgrade.");
                    }
                    if (b.getLevels().get(level + 1).getPriceGold() > cityResourcesManager.getResourceCount(cityId, ResourceType.Gold)) {
                        ok = false;
                        error("You do not have enough gold to upgrade.");
                    }
                    if (b.getLevels().get(level + 1).getPriceStone() > cityResourcesManager.getResourceCount(cityId, ResourceType.Stone)) {
                        ok = false;
                       error("You do not have enough stone to upgrade.");
                    }
                    for (BuildingType buildingType : b.getLevels().get(level+1).getRequires().keySet()) {
                        for (BuildingInCity building : cityBuildingManager.getAllBuildingsInCityOfType(cityId, buildingType)) {
                            if (building.getLevel() >= b.getLevels().get(level+1).getRequires().get(buildingType)) {
                                requirements = true;
                                break;
                            }
                        }
                    }
                    if (ok == true && requirements == true) {
                        BuildingInCity toBuild = buildingConvertor.getBuildingFromXML(name);
                        toBuild.setLevel(level + 1);

                        cityBuildingManager.buildBuilding(cityId, toBuild, c);
                        cityResourcesManager.lowerResource(cityId, ResourceType.Food, toBuild.getLevels().get(level+1).getPriceFood());
                        cityResourcesManager.lowerResource(cityId, ResourceType.Gold, toBuild.getLevels().get(level+1).getPriceGold());
                        cityResourcesManager.lowerResource(cityId, ResourceType.Stone, toBuild.getLevels().get(level+1).getPriceStone());

                        setResponsePage(BuildingPage.class, parameters);
                    }
                    if(requirements != true){
                        error("Requirements to upgrade are not met.");
                    }
                }

                
            }
        };
        
        add(form);
    }
}
